package ru.osu.acm.common.config;

import ru.osu.acm.testing.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * User: Aleksey
 * Date: 17.02.2009
 * Time: 19:31:24
 */
public class Config {
    public static HashMap<String, Compiler> compilers = new HashMap<String, Compiler>();
    public static String database;
    public static String username;
    public static String password;
    public static String driver;
    public static String uri;
    public final static String jndi = "jdbc/olga";

    public static String checkerCompileString;
    public static String checkerExecuteString;

    public static String archiverExtractString;

    public static void loadConfig(String fileName) {
        File configFile = new File(fileName);
        HashMap<String, String> options = Utils.getList("config.options", configFile).get(0);
        database = options.get("database");
        username = options.get("username");
        password = options.get("password");
        driver = options.get("driver");
        uri = options.get("uri");

        HashMap<String, String> checkerOptions = Utils.getList("config.checkers", configFile).get(0);
        checkerCompileString = checkerOptions.get("compilestring");
        checkerExecuteString = checkerOptions.get("executestring");

        HashMap<String, String> archiverOptions = Utils.getList("config.archiver", configFile).get(0);
        archiverExtractString = archiverOptions.get("extractstring");

        ArrayList<HashMap<String, String>> compilerz = Utils.getList("config.compilers.compiler", configFile);

        for (HashMap<String, String> hm : compilerz) {
            ArrayList<String> legalFiles = new ArrayList<String>();
            String[] temp = hm.get("legalfiles").split(";");
            legalFiles.addAll(Arrays.asList(temp));

            compilers.put(hm.get("code"), new Compiler(
                    hm.get("name"),
                    hm.get("description"),
                    hm.get("extension"),
                    hm.get("compilestring"),
                    hm.get("executestring"),
                    legalFiles
            ));
        }
    }
}
